#include "mainwindow.h"

#include <QApplication>
#include<QMediaPlayer>
#include<QVideoWidget>
_EXTERN_C
#include<libavutil/log.h>
#include<libavcodec/avcodec.h>
#include<libavformat/avformat.h>
#include<libavutil/opt.h>
#include<libswscale/swscale.h>
_END_EXTERN_C
#include<QThreadPool>

int main(int argc, char *argv[])
{

    qDebug()<<avcodec_configuration();
//    av_register_all();
    //没有也可以用
//    avformat_network_init();

    AVFormatContext *pFormat = nullptr;
    const char* path = "F:/迅雷下载/yyyyy.avi";

//    const char *path = "http://ivi.bupt.edu.cn/hls/cctv1hd.m3u8";
//    const char *path = "https://vd2.bdstatic.com/mda-mh44wa1igqbjerep/720p/h264/1628134143804856424/mda-mh44wa1igqbjerep.mp4?v_from_s=hkapp-haokan-nanjing&auth_key=1637504713-0-0-1f4251db357fadbd5cfcd5585d306e3b&bcevod_channel=searchbox_feed&pd=1&pt=3&abtest=&klogid=3313575328";

//    AVDictionary *opt = nullptr;
//    av_dict_set(&opt,"rtsp_transport","tcp",0);
//    av_dict_set(&opt,"max_delay","550",0);

    int ret = avformat_open_input(&pFormat,path,nullptr,nullptr);
//    int ret = avformat_open_input(&pFormat,path,nullptr,&opt);
    if(ret){
        qDebug()<<"avformat_open_input failed";
        return -1;

    }
    qDebug()<<"avformat_open_input success";

    ret = avformat_find_stream_info(pFormat,nullptr);
    if(ret){
        qDebug()<<"avformat_find_stream_info failed";
        return -1;

    }
    qDebug()<<"avformat_find_stream_info success";


    int time = pFormat->duration;
    int mbittime = (time/1000000)/60;
    int mmintime = (time/1000000)%60;

    qDebug()<<mbittime << "分"<<mmintime << "秒";

    av_dump_format(pFormat,0,path,0);

    //寻找流 mp4 h264  aac
    int videoStream = -1,audioStream = - 1;

    videoStream = av_find_best_stream(pFormat,AVMEDIA_TYPE_VIDEO,-1,-1,nullptr,0);
    audioStream = av_find_best_stream(pFormat,AVMEDIA_TYPE_AUDIO,-1,-1,nullptr,0);

    AVCodec * avCodec =avcodec_find_decoder(pFormat->streams[videoStream]->codec->codec_id);
    if(!avCodec){
        qDebug()<<"avcodec_find_decoder failed";
    }
    qDebug()<<"avcodec_find_decoder success";


    ret = avcodec_open2(pFormat->streams[videoStream]->codec,avCodec,nullptr);

    if(ret){
        qDebug()<<"avcodec_open2 failed";
        return -1;

    }
    qDebug()<<"avcodec_open2 success";

    //帧
    AVFrame *frame = av_frame_alloc();
    AVFrame *frameYUV = av_frame_alloc();

    int width = pFormat->streams[videoStream]->codec->width;
    int height = pFormat->streams[videoStream]->codec->height;
    AVPixelFormat fmt = pFormat->streams[videoStream]->codec->pix_fmt;

    int nSize = avpicture_get_size(AV_PIX_FMT_YUV420P,width,height);

    //
    uint8_t * buff=(uint8_t*)av_malloc(nSize);
    avpicture_fill((AVPicture *)frameYUV,buff,AV_PIX_FMT_YUV420P,width,height);

    AVPacket *packet = (AVPacket*)av_malloc(sizeof(AVPacket));

    //转换上下文
//    sws_getCachedContext()
    SwsContext *swsCtx =sws_getContext(width,height,fmt,width,height,AV_PIX_FMT_YUV420P,SWS_BICUBIC,nullptr,nullptr,nullptr);
    int go = 0;
    int frameCount = 0;
//    while(av_read_frame(pFormat,packet)>=0){
//        //视频流？
//        if(packet->stream_index == AVMEDIA_TYPE_VIDEO){
//            ret = avcodec_decode_video2(pFormat->streams[videoStream]->codec,frame,&go,packet);
//            if(ret<0){
//                qDebug()<<"avcodec_decode_video2 failed";
//                return -1;

//            }
//            qDebug()<<"avcodec_decode_video2 success";

//            if(go){
//                sws_scale(swsCtx,frame->data,frame->linesize,0,0,frameYUV->data,frameYUV->linesize);
//                frameCount ++;
//                qDebug()<<"frame count:"<<frameCount;
//            }
//        }
//        av_free_packet(packet);
//    }

    sws_freeContext(swsCtx);
    av_frame_free(&frame);
    av_frame_free(&frameYUV);
//    avcodec_close();
    avformat_close_input(&pFormat);




    av_log_set_level(AV_LOG_DEBUG);
    av_log(nullptr,AV_LOG_DEBUG,"%s","Hello World!\n");
















    class T0:public QRunnable{

    protected:
        void run() override{
            av_log_set_level(AV_LOG_DEBUG);
            av_log(nullptr,AV_LOG_DEBUG,"%s","Hello World!\n");
        }

    };





    QThreadPool::globalInstance()->start(new T0());



    QApplication a(argc, argv);



    QMediaPlayer *player = new QMediaPlayer;

    QVideoWidget *video = new QVideoWidget;
    video->resize(500,500);
    video->show();

    player->setVideoOutput(video);

//    player->setMedia(QUrl::fromLocalFile("test.mp4"));
    player->setMedia(QUrl::fromLocalFile("F:/迅雷下载/yyyyy.avi"));
    player->setVolume(50);
    player->play();

    MainWindow w;
    w.show();



    return a.exec();


}
